package com.apes.fn.scm.purchaseReturn.model;

import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.Model;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import com.apes.framework.plugin.soe.annotations.State;
import com.apes.framework.plugin.soe.annotations.States;
import com.apes.scm.masterdata.enumeration.model.Enumeration;
import com.apes.scm.masterdata.party.model.PartyRole;
import com.apes.scm.masterdata.stock.model.Location;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.*;
import javax.validation.constraints.NotNull;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

/**
 * @BelongsProject: apes-server
 * @BelongsPackage: com.apes.fn.scm.purchaseReturn.model
 * @ClassName: PurchaseReturnApproval
 * @Author: pyb
 * @CreateTime: 2020-01-19 15:54
 * @Description: 返厂审批
 * @Version: 1.0
 */
@Getter
@Setter
@SelectBeforeUpdate
@DynamicInsert
@DynamicUpdate
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = PurchaseReturnApproval.CONST_TABLE_NAME)
public class PurchaseReturnApproval extends Model {
    public static final String CONST_TABLE_NAME = "purchase_return_approval";

    @Id
    @Column(name = "id")
    @ApesGenerator(code = CONST_TABLE_NAME, name = "返厂申请审批单", prefix = "SH%(company)%(yy)%(month)", padding = 5, useDateRange=true)
    private String id;
    @NotNull
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "company_id", referencedColumnName = "id")
    @Comment("公司")
    private PartyRole company;

    @NotNull
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "ship_location_id", referencedColumnName = "id")
    @Comment("发货仓库")
    private Location shipLocation;

    @NotNull
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "turnover_location_id", referencedColumnName = "id")
    @Comment("周转仓库")
    private Location turnoverLocation;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "move_type", referencedColumnName = "id")
    @Comment("返厂原因")
    private Enumeration returnReason;

    @Column(name = "note")
    @Comment("备注")
    private String note;

    @Column(name = "derived_document")
    @Comment("衍生单据")
    private String derivedDocument;
//
//    @Column(name = "apply_document")
//    @Comment("申请单据")
//    private String applyDocument;

    @Column(name = "callback_document")
    @Comment("回调单据")
    private String callbackDocument;

    @Column(name = "state", length = 10, nullable = false)
    @States(name = "订单状态", values = {
            @State(id = "sent", name = "新建"),
            @State(id = "oa", name = "审核"),
            @State(id = "approve", name = "采购审核"),
            @State(id = "disposing", name = "处理中"),
            @State(id = "done", name = "已完成"),
            @State(id = "consent", name = "已同意"),
            @State(id = "cancel", name = "已驳回")})
    private String state;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "approve_uid", referencedColumnName = "id")
    @Comment("审批人")
    private PartyRole approveUid;

    @Column(name = "date_approve")
    @Comment("审批日期")
    private Date dateApprove;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "purchase_approve_uid", referencedColumnName = "id")
    @Comment("采购审批人")
    private PartyRole purchaseApproveUid;

    @Column(name = "date_purchase_approve")
    @Comment("采购审批日期")
    private Date datePurchaseApprove;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "finance_approve_uid", referencedColumnName = "id")
    @Comment("财务审批人")
    private PartyRole financeApproveUid;

    @Column(name = "date_finance_approve")
    @Comment("财务审批日期")
    private Date dateFinanceApprove;

    @OneToMany(mappedBy = "purchaseReturnApproval", cascade = CascadeType.ALL, orphanRemoval = true)
    private Set<PurchaseReturnApprovalItem> purchaseReturnApprovalItems = new HashSet<>();

    public Set<PurchaseReturnApprovalItem> getPurchaseReturnApprovalItems() {
        for (PurchaseReturnApprovalItem item : purchaseReturnApprovalItems) {
            item.setPurchaseReturnApproval(this);
        }
        return purchaseReturnApprovalItems;
    }
}
